﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using APP.HELPER.Utils.Language;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using Dictionary.ERP;
using Common;
using Security.ERP;
using Extra.ERP;
using Common.Class;

namespace Dictionary
{
    public class xucEducation : xucBaseBasic
    {
        private IContainer components;

        public xucEducation()
        {
            InitializeComponent();
            base.ucToolBar.SetInterface();
        }

        protected override void Add()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiEducation") == "OK")
            {
                if (!MyRule.AllowAdd)
                {
                    MyRule.Notify();
                }
                else
                {
                    var add = new xfmEducationAdd(Common.Class.Actions.Add);
                    add.Added += frm_Added;
                    add.ShowDialog();
                }
            }
        }

        private void AddRow(DIC_EDUCATION Item)
        {
            AdvBandedGridView gbList = base.gbList;
            int focusedRowHandle = gbList.FocusedRowHandle;
            gbList.AddNewRow();
            focusedRowHandle = gbList.FocusedRowHandle;
            gbList.SetRowCellValue(focusedRowHandle, "Active", Item.Active);
            gbList.SetRowCellValue(focusedRowHandle, "EducationCode", Item.EducationCode);
            gbList.SetRowCellValue(focusedRowHandle, "EducationName", Item.EducationName);
            gbList.SetRowCellValue(focusedRowHandle, "Description", Item.Description);
            gbList.UpdateCurrentRow();
        }

        public override void Change()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiEducation") == "OK")
            {
                if (!MyRule.AllowAccess)
                {
                    MyRule.Notify();
                }
                else
                {
                    var item = new DIC_EDUCATION();
                    object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "EducationCode");
                    if (cellValue != null)
                    {
                        base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                        if (item.Get(cellValue.ToString()) != "OK")
                        {
                            this.DoHide();
                            XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            this.DoHide();
                            var add = new xfmEducationAdd(Common.Class.Actions.Update, item);
                            add.Updated += frm_Updated;
                            add.Added += frm_Added;
                            add.ShowDialog();
                        }
                    }
                }
            }
        }

        public override void Delete()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiEducation") == "OK")
            {
                if (!MyRule.AllowDelete)
                {
                    MyRule.Notify();
                }
                else if (!ClsOption.System2.IsQuestion ||
                         (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o",
                             MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No))
                {
                    object cellValue;
                    base.SetWaitDialogCaption("Đang x\x00f3a...");
                    string str = "";
                    bool flag = false;
                    AdvBandedGridView gbList = base.gbList;
                    int[] selectedRows = gbList.GetSelectedRows();
                    var dic_education = new DIC_EDUCATION();
                    for (int i = selectedRows.Length; i > 0; i--)
                    {
                        flag = true;
                        cellValue = base.GetCellValue(selectedRows[i - 1], "EducationCode");
                        if (cellValue != null)
                        {
                            SYS_LOG.Insert("Danh Mục Học Vấn", "Xo\x00e1", cellValue.ToString());
                            str = dic_education.Delete(cellValue.ToString());
                            if (str == "OK")
                            {
                                gbList.DeleteRow(selectedRows[i - 1]);
                            }
                            else if (str != "OK")
                            {
                                MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str,
                                    "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                        }
                    }
                    this.DoHide();
                    if (!flag && (gbList.DataSource != null))
                    {
                        var args = new Common.Class.RowClickEventArgs((gbList == null) ? -1 : gbList.FocusedRowHandle,
                            (gbList.FocusedColumn == null) ? -1 : gbList.FocusedColumn.ColumnHandle,
                            (gbList.FocusedColumn == null) ? "" : gbList.FocusedColumn.FieldName);
                        base.m_RowClickEventArgs = args;
                        cellValue = null;
                        cellValue = base.GetCellValue(args.RowIndex, "EducationCode");
                        if (cellValue != null)
                        {
                            SYS_LOG.Insert("Danh Mục Học Vấn", "Xo\x00e1", cellValue.ToString());
                            base.SetWaitDialogCaption("Đang x\x00f3a...");
                            str = dic_education.Delete(cellValue.ToString());
                            if (str == "OK")
                            {
                                gbList.DeleteRow(args.RowIndex);
                            }
                            else if (str != "OK")
                            {
                                MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str,
                                    "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                            this.DoHide();
                        }
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override bool ExportPermision()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiEducation") != "OK")
            {
                return false;
            }
            if (!MyRule.AllowExport)
            {
                MyRule.Notify();
                return false;
            }
            SYS_LOG.Insert("Danh Mục Học Vấn", "Xuất");
            return base.ExportPermision();
        }

        private void frm_Added(object sender, DIC_EDUCATION e)
        {
            AddRow(e);
        }

        private void frm_Updated(object sender, DIC_EDUCATION Item)
        {
            UpdateRow(Item, base.m_RowClickEventArgs);
        }

        protected override void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            var view = (AdvBandedGridView) sender;
            var args = new Common.Class.RowClickEventArgs((view == null) ? -1 : view.FocusedRowHandle,
                (view.FocusedColumn == null) ? -1 : view.FocusedColumn.ColumnHandle,
                (view.FocusedColumn == null) ? "" : view.FocusedColumn.FieldName);
            base.m_RowClickEventArgs = args;
            object cellValue = base.GetCellValue(args.RowIndex, "EducationCode");
            base.DisableMenu(false);
            if (cellValue == null)
            {
                base.DisableMenu(true);
            }
        }

        private void InitializeComponent()
        {
            components = new Container();
            base.AutoScaleMode = AutoScaleMode.Font;
        }

        protected override void List_Init(AdvBandedGridView dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string fieldName = dt.Columns[i].FieldName;
                if (fieldName == null)
                {
                    goto Label_0275;
                }
                if (!(fieldName == "EducationCode"))
                {
                    if (fieldName == "EducationName")
                    {
                        goto Label_0108;
                    }
                    if (fieldName == "Description")
                    {
                        goto Label_01C0;
                    }
                    goto Label_0275;
                }
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = MultiLanguages.GetString("tbl_FormBasic", "ID", "M\x00e3");
                }
                dt.Columns[i].Width = 60;
                continue;
                Label_0108:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = MultiLanguages.GetString("tbl_FormBasic", "Name", "T\x00ean");
                }
                dt.Columns[i].Width = 180;
                continue;
                Label_01C0:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = MultiLanguages.GetString("tbl_FormBasic", "Description", "Ghi ch\x00fa");
                }
                dt.Columns[i].Width = 150;
                continue;
                Label_0275:
                dt.Columns[i].Visible = false;
            }
        }

        protected override void Print()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiEducation") == "OK")
            {
                if (!MyRule.AllowPrint)
                {
                    MyRule.Notify();
                }
                else
                {
                    SYS_LOG.Insert("Danh Mục Học Vấn", "In");
                    base.Print();
                }
            }
        }

        public override void ReLoad()
        {
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            base.gcList.DataSource = new DIC_EDUCATION().GetList();
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            List_Init(base.gbList);
            base.SetWaitDialogCaption("Nạp quyền sử dụng...");
            MyRule.Get(MyLogin.RoleId, "bbiEducation");
            if (!MyRule.AllowPrint)
            {
                base.ucToolBar.bbiPrint.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiEducation");
            if (!MyRule.AllowExport)
            {
                base.ucToolBar.bbiExport.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiEducation");
            if (!MyRule.AllowAdd)
            {
                base.ucToolBar.bbiAdd.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiEducation");
            if (!MyRule.AllowDelete)
            {
                base.ucToolBar.bbiDelete.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiEducation");
            if (!MyRule.AllowEdit)
            {
                base.ucToolBar.bbiEdit.Visibility = BarItemVisibility.Never;
            }
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        private void UpdateRow(DIC_EDUCATION item, Common.Class.RowClickEventArgs e)
        {
            AdvBandedGridView gbList = base.gbList;
            int rowIndex = e.RowIndex;
            gbList.SetRowCellValue(rowIndex, "Active", item.Active);
            gbList.SetRowCellValue(rowIndex, "EducationCode", item.EducationCode);
            gbList.SetRowCellValue(rowIndex, "EducationName", item.EducationName);
            gbList.SetRowCellValue(rowIndex, "Description", item.Description);
            gbList.UpdateCurrentRow();
        }

        private void xucCustomerType_Load(object sender, EventArgs e)
        {
        }
    }
}